package statis

import (
	"errors"

	"qkstatis/errorcode"

	log "github.com/cihub/seelog"
)

var statisInstance *StatisService

type StatisService struct{}

func GetStatisService() *StatisService {
	if statisInstance == nil {
		statisInstance = &StatisService{}
	}
	return statisInstance
}

//营收表数据
func (p *StatisService) GetAllProfit(req *ShowItemReq, resp *ShowItemResp, ltype int, timeRange string) error {
	ok, start, end := p.IsValidTimeRange(ltype, timeRange)
	if !ok {
		log.Errorf("无效的时间范围, ltype:%d, timeRange:%s", ltype, timeRange)
		resp.Code = errorcode.EC_QKSTATIS_INVALID_TIME_RANGE
		return errors.New("无效的时间范围")
	}
	conditions := map[string]interface{}{
		"date_flag >= ?": start,
		"date_flag <= ?": end,
	}
	data, total := GetAllProfitStatisService().ListAllProfit(ltype, conditions, !req.Role)
	for _, dt := range data {
		resp.Data = append(resp.Data, dt)
	}
	resp.Code = errorcode.EC_GL_SUCCESS
	resp.Msg = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	resp.PageIndex = req.PageIndex
	resp.PageSize = req.PageSize
	resp.Total = total
	if total > req.PageSize {
		resp.HasMore = true
	} else {
		resp.HasMore = false
	}
	return nil
}

//洗衣点数据
func (p *StatisService) GetLWProfit(req *ShowItemReq, resp *ShowItemResp, ltype int, timeRange, leagueId, washRoomId, sessionId string) error {
	ok, start, end := p.IsValidTimeRange(ltype, timeRange)
	if !ok {
		log.Errorf("无效的时间范围, ltype:%d, timeRange:%s", ltype, timeRange)
		resp.Code = errorcode.EC_QKSTATIS_INVALID_TIME_RANGE
		return errors.New("无效的时间范围")
	}
	conditions := map[string]interface{}{
		"date_flag >= ?": start,
		"date_flag <= ?": end,
	}

	//以洗衣点粒度显示数据
	if washRoomId != "0000" {
		conditions["wash_room_id = ?"] = washRoomId
		data, total := GetWashRoomProfitStatisService().ListWashRoomProfit(sessionId, ltype, conditions, !req.Role)
		resp.Total = total
		for _, dt := range data {
			resp.Data = append(resp.Data, dt)
		}
		log.Infof("resp.Data:%s",resp.Data)
	} else { //以加盟商粒度显示数据
		if leagueId != "0000" {
			conditions["league_id = ?"] = leagueId
		}
		data, total := GetLeagueProfitStatisService().ListLeagueProfit(sessionId, ltype, conditions, !req.Role)
		resp.Total = total
		for _, dt := range data {
			resp.Data = append(resp.Data, dt)
		}
	}
	resp.Code = errorcode.EC_GL_SUCCESS
	resp.Msg = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	resp.PageIndex = req.PageIndex
	resp.PageSize = req.PageSize
	if resp.Total > req.PageSize {
		resp.HasMore = true
	} else {
		resp.HasMore = false
	}
	return nil
}

//洗衣机数据
func (p *StatisService) GetWasherProfit(req *ShowItemReq, resp *ShowItemResp, ltype int, timeRange, washerId string) error {
	ok, start, end := p.IsValidTimeRange(ltype, timeRange)
	if !ok {
		log.Errorf("无效的时间范围, ltype:%d, timeRange:%s", ltype, timeRange)
		resp.Code = errorcode.EC_QKSTATIS_INVALID_TIME_RANGE
		return errors.New("无效的时间范围")
	}
	conditions := map[string]interface{}{
		"date_flag >= ?": start,
		"date_flag <= ?": end,
	}

	conditions["washer_id = ?"] = washerId
	data, total := GetWasherProfitStatisService().ListWasherProfit(ltype, conditions, !req.Role)
	resp.Total = total
	for _, dt := range data {
		resp.Data = append(resp.Data, dt)
	}
	resp.Code = errorcode.EC_GL_SUCCESS
	resp.Msg = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	resp.PageIndex = req.PageIndex
	resp.PageSize = req.PageSize
	if resp.Total > req.PageSize {
		resp.HasMore = true
	} else {
		resp.HasMore = false
	}
	return nil
}
